package com.szpxt.action;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.opensymphony.xwork2.ActionSupport;
import com.szpxt.model.Organization;
import com.szpxt.service.OrganizationService;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Component @Scope("prototype")
public class OrganizationAction extends ActionSupport implements ServletRequestAware, ServletResponseAware, SessionAware {
	
	private HttpServletRequest request;
	private HttpServletResponse response;
	private Map<String, Object> sessionMap;
	
	private OrganizationService organizationService;
	
	public void getTree() throws IOException {
		JSONArray array = new JSONArray();
		
		List<Organization> listRoot = organizationService.getRoot();
		for(Organization element: listRoot) {
			array.add(recursive(element));
		}
		
		response.setContentType("application/json;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.getWriter().write(array.toString());
		response.getWriter().flush();
		response.getWriter().close();
	}
	
	private JSONObject recursive(Organization organization) {
		JSONObject object = new JSONObject();
		object.put("id", organization.getId());
		object.put("text", organization.getName());
		
		if(organization.getChildren().size() > 0) {
			JSONArray array = new JSONArray();
			for(Organization element: organization.getChildren()) {
				array.add(recursive(element));
			}
			
			object.put("children", array);
			object.put("expanded", true);
		}
		else {
			object.put("leaf", true);
		}
		
		return object;
	}
	
	public void saveOrUpdate() throws IOException {
		//提取参数
		Map<String, String> map = new HashMap<String, String>();
		map.put("id", request.getParameter("id"));
		map.put("name", request.getParameter("name"));
		map.put("parentId", request.getParameter("parentId"));
		Organization organization = organizationService.saveOrUpdate(map);		//保存
		
		//返回
		JSONObject joData = new JSONObject();
		joData.put("id", organization.getId());
		joData.put("name", organization.getName());
		
		JSONObject jo = new JSONObject();
		jo.put(SUCCESS, true);
		jo.put("data", joData);
		
		response.setContentType("application/json;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.getWriter().write(jo.toString());
		response.getWriter().flush();
		response.getWriter().close();
	}
	
	public void get() throws IOException {
		long id = Long.parseLong(request.getParameter("id"));
		Organization organization = organizationService.get(id);
		
		JSONObject joData = new JSONObject();
		joData.put("id", organization.getId());
		joData.put("name", organization.getName());
		joData.put("parentId", organization.getParent().getId());
		joData.put("parent", organization.getParent().getName());
		
		JSONObject jo = new JSONObject();
		jo.put(SUCCESS, true);
		jo.put("data", joData);
		
		response.setContentType("application/json;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.getWriter().write(jo.toString());
		response.getWriter().flush();
		response.getWriter().close();
	}
	
	public void delete() throws IOException {
		long id = Long.parseLong(request.getParameter("id"));
		organizationService.delete(id);
		
		JSONObject jo = new JSONObject();
		jo.put(SUCCESS, true);
		
		response.setContentType("application/json;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.getWriter().write(jo.toString());
		response.getWriter().flush();
		response.getWriter().close();
	}

	public void logout() throws IOException {
		sessionMap.clear();
		
		JSONObject jo = new JSONObject();
		jo.put(SUCCESS, true);
		
		response.setContentType("application/json;UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.getWriter().write(jo.toString());
		response.getWriter().flush();
		response.getWriter().close();
	}
	
	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}
	
	@Override
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}

	//获取和设置方法
	public OrganizationService getOrganizationService() {
		return organizationService;
	}

	public void setOrganizationService(OrganizationService organizationService) {
		this.organizationService = organizationService;
	}

	@Override
	public void setSession(Map<String, Object> sessionMap) {
		this.sessionMap = sessionMap;
	}

}
